using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
using QS.Logic.Objects;
using QS.Input;
using QS.Editor;
using QS.Game.Camera;
using DPSF.ParticleSystems;
using QS.Game.Logic.Objects;
using DPSF;

namespace QS
{
    class Game1 : GameScreen
    {
        private ContentManager content;

        private QCoordinateSystem m_CoordinateSystem;
        private QGrid m_Grid;
        private QSkyBox m_SkyBox;

        private QSpaceShip m_SpaceShip;

        private QCameraManager m_CameraManager;

        public Game1()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            m_CoordinateSystem = new QCoordinateSystem();
            m_CoordinateSystem.Load(ScreenManager.GraphicsDevice);

            m_Grid = new QGrid();
            m_Grid.Load(ScreenManager.GraphicsDevice,100,100);

            m_SkyBox = new QSkyBox();
            m_SkyBox.Load(ScreenManager.GraphicsDevice, content);

            m_SpaceShip = QObjectFactory.GetInstance(typeof(QSpaceShip), ScreenManager.GraphicsDevice, content, null) as QSpaceShip;

            m_CameraManager = new QCameraManager();
            m_CameraManager.Load(ScreenManager.GraphicsDevice, content, m_SpaceShip);

            QBulletManager.getInstance().Load(ScreenManager.GraphicsDevice, content);

            ScreenManager.Game.ResetElapsedTime();
            m_Box = new QBox();
            m_Box.Load(ScreenManager.GraphicsDevice);

            QMouseOperations.ResetMouse = true;
        }
        QBox m_Box;

        public override void UnloadContent()
        {
            content.Unload();
        }

        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // Look up inputs for the active player profile.
            int playerIndex = (int)ControllingPlayer.Value;

            KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
            GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];

            // The game pauses either if the user presses the pause button, or if
            // they unplug the active gamepad. This requires us to keep track of
            // whether a gamepad was ever plugged in, because we don't want to pause
            // on PC if they are playing with a keyboard and have no gamepad at all!
            bool gamePadDisconnected = !gamePadState.IsConnected &&
                                       input.GamePadWasConnected[playerIndex];

            if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
            {
                ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
            }
        }

        private bool FirstFrame = true;

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            if (IsActive)
            {
                QKeyboardOperations.UpdateKeyboardState();
                QMouseOperations.UpdateMouseState(ScreenManager.GraphicsDevice);
                if (!FirstFrame)
                {
                    m_SpaceShip.Update(gameTime);
                    QBulletManager.getInstance().Update(gameTime);
                    m_CameraManager.Update(gameTime);
                }
                else
                    FirstFrame = false;

            }
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.Clear(Color.Black);
            QCamera camera = m_CameraManager.Camera;


            ScreenManager.GraphicsDevice.BlendState = BlendState.Opaque;
            ScreenManager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            m_SkyBox.Draw(ScreenManager.GraphicsDevice, camera, ScreenManager.SpriteBatch);

            m_Grid.Draw(ScreenManager.GraphicsDevice, camera.ViewMatrix, camera.ProjectionMatrix, Matrix.Identity);
            m_CoordinateSystem.Draw(ScreenManager.GraphicsDevice, camera.ViewMatrix, camera.ProjectionMatrix);
            m_Box.Draw(ScreenManager.GraphicsDevice, camera.ViewMatrix, camera.ProjectionMatrix, Matrix.Identity);

            m_SpaceShip.Draw(ScreenManager.GraphicsDevice, camera, ScreenManager.SpriteBatch);

            QBulletManager.getInstance().Draw(ScreenManager.GraphicsDevice, camera, ScreenManager.SpriteBatch);


            base.Draw(gameTime);
        }
    }
}
